package com.study.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import org.jetbrains.annotations.NotNull;

public abstract class LazyBaseFragment extends Fragment {
    private static final String STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN";
    //Fragment是否可见
    private boolean isVisiable = false;
    //是否是第一次加载
    private boolean isFirstLoad = true;
    //view是否初始化毕
    private boolean isInited = false;


    protected Activity mContext;
    protected View mRootView;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            boolean isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (isSupportHidden) {
                ft.hide(this);
            } else {
                ft.show(this);
            }
            ft.commit();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(layoutId(), null);
        initView();
        initData();
        return mRootView;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());
    }

    @Override
    public void onAttach(@NotNull Context context) {
        super.onAttach(context);
        this.mContext = (Activity) context;
    }

    protected void initView() {
        isInited = true;
        lazyLoad();
    }

    protected void initData() {
    }


    protected abstract int layoutId();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFirstLoad = true;
    }

    //与ViewPager一起使用，调用的此方法
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisiable = true;
            onFragmentVisiable();
        } else {
            isVisiable = false;
            onFragmentHide();
        }
    }

    //FragmentTransaction调用
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            onFragmentHide();
            isVisiable = false;
        } else {
            isVisiable = true;
            onFragmentVisiable();
        }
    }


    //懒加载
    protected abstract void lazyInitDatas();

    //fragment显示时回调
    protected void onFragmentVisiable() {
        lazyLoad();
    }

    //fragment隐藏式回调
    protected void onFragmentHide() {

    }

    //数据的懒加载
    private void lazyLoad() {
        if (isVisiable && isInited && isFirstLoad) {
            lazyInitDatas();
            isFirstLoad = false;
        }
    }
}